package com.chenpeijian.demo.demos.web.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * @program: student_code
 * @description: 订单服务
 * @author: 陈培建
 * @create: 2024-12-24 11:27
 **/
@Slf4j
@Component
public class OrderFutureService {
    public HashMap<String,Object> asyncLogin(){
        HashMap<String,Object> map = new HashMap<>();
        long startTime = System.currentTimeMillis();
        log.info("开始查询用户角色信息！");
        CompletableFuture<Map<String, Object>> roleFuture = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Map<String, Object> roleMap = new HashMap<String, Object>();
            roleMap.put("role", "管理员");
            long endTime = System.currentTimeMillis();
            log.info("查询用户角色信息耗时:" + (endTime - startTime)  + "毫秒");
            return roleMap;
        });

        log.info("开始查询用户菜单信息！");
        CompletableFuture<Map<String, Object>> menuFuture = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Map<String, Object> menuMap = new HashMap<String, Object>();
            menuMap.put("menu", "首页，账户管理，积分管理");
            long endTime = System.currentTimeMillis();
            log.info("查询用户菜单信息耗时:" + (endTime - startTime)  + "毫秒");
            return menuMap;
        });

        log.info("开始查询用户余额信息！");
        CompletableFuture<Map<String, Object>> amountFuture = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Map<String, Object> amountMap = new HashMap<String, Object>();
            amountMap.put("amount", 1999);
            long endTime = System.currentTimeMillis();
            log.info("查询用户余额信息耗时:" + (endTime - startTime)  + "毫秒");
            return amountMap;
        });

        log.info("开始查询用户积分信息！");
        CompletableFuture<Map<String, Object>> integralFuture = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Map<String, Object> integralMap = new HashMap<String, Object>();
            integralMap.put("integral", 1015);
            long endTime = System.currentTimeMillis();
            log.info("查询用户积分信息耗时:" + (endTime - startTime)  + "毫秒");
            return integralMap;
        });

        Map<String, Object> roleMap = roleFuture.join();
        Map<String, Object> menuResult = menuFuture.join();
        Map<String, Object> amountResult = amountFuture.join();
        Map<String, Object> pointsResult = integralFuture.join();
        // 合并 roleMap、menuResult、amountResult 和 pointsResult 到 map
        map.putAll(roleMap);
        map.putAll(menuResult);
        map.putAll(amountResult);
        map.putAll(pointsResult);
        map.put("查询用户全部信息总耗时",System.currentTimeMillis() - startTime);
        log.info("查询用户全部信息总耗时：" + (System.currentTimeMillis() - startTime)  + "毫秒");
        return map;
    }
}
